/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dawnland02.model.task;

import dawnland02.model.action.Action;
import dawnland02.model.entity.EntityInternal;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Petru Obreja (obrejap@yahoo.com)
 */
public class BaseTask extends Task{
    
    private List<Action> actions; //actions which will be handled by this task

    public BaseTask( String name, EntityInternal entity) {
        super(name, entity);
        actions = new LinkedList<Action>();
    }

    public void addAction(Action action){
        actions.add(action);
    }

    public Action getAction(Integer index){
        if(index>=actions.size()){
            return null;
        }
        return actions.get(index);
    }

    @Override
    public void execute() {
        if(getTaskStatus().compareTo(TaskStatus.PREPARED)<0){
            prepare();            
        }
        if(getTaskStatus() == TaskStatus.PREPARED){
            for(Action action: actions){
                action.getActionController().start();
            }
            if(actions.size()==0){
                end();
            }
        } else {
            end();
        }
    }

    @Override
    protected void prepare() {
        //do nothing; it will be overwritten by subclasses
        setTaskStatus(TaskStatus.PREPARED);
    }

    @Override
    public boolean isGroupTask() {
        return false;
    }

    @Override
    protected void init() {
        //do nothing; it will be overwritten by subclasses
    }

    @Override
    public void end() {
        setTaskStatus(TaskStatus.FINISHED);
        executeNextTaskFromParent();
    }
}
